package com.fsnbigdata.util;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public class AliOSSUtil {

    @Value("${aliyun.oss.endpoint}")
    private String       END_POINT         = "";

    @Value("${aliyun.oss.access-key}")
    private String       ACCESS_KEY_ID     = "";

    @Value("${aliyun.oss.access-secret}")
    private String       ACCESS_KEY_SECRET = "";

    @Value("${aliyun.oss.bucket}")
    private String       BUCKET_NAME       = "";

    // 文件访问域名
    @Value("${aliyun.oss.host}")
    private String       FILE_HOST         = "";

    private SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

    public String uploadImage(MultipartFile file) throws IOException{
        return upload(file, "images");
    }

    public String uploadVideo(MultipartFile file) throws IOException{
        return upload(file, "videos");
    }

    /**
     * 方法描述:上传文件
     *
     * @author leon 2016年12月16日 上午11:40:34
     * @param file 文件对象
     * @return
     */
    public String upload(MultipartFile file, String prefix) throws IOException {
        if (file == null) {
            return null;
        }
        // 创建OSS客户端
        OSSClient ossClient = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        try {
            // 判断文件容器是否存在，不存在则创建
            if (!ossClient.doesBucketExist(BUCKET_NAME)) {
                ossClient.createBucket(BUCKET_NAME);
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(BUCKET_NAME);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(createBucketRequest);
            }
            // 创建文件路径

            String fileUrl = new String(prefix + "/" + format.format(new Date()) + "/" + UUID.randomUUID()) + "." + FilenameUtils.getExtension(file.getOriginalFilename());
            // 上传文件
            PutObjectResult result = ossClient.putObject(new PutObjectRequest(BUCKET_NAME, fileUrl, file.getInputStream()));
            if (null != result) {
                return FILE_HOST + fileUrl;
            }
        } catch (OSSException oe) {

        } catch (ClientException ce) {

        } finally {
            // 关闭OSS服务，一定要关闭
            ossClient.shutdown();
        }
        return null;
    }

    /**
     * 方法描述:上传文件
     *
     * @author leon 2016年12月26日 下午3:33:13
     * @param inputStream 文件流
     * @return
     * @throws FileNotFoundException
     */
    public String upload(InputStream inputStream) throws FileNotFoundException {
        if (inputStream == null) {
            return null;
        }
        // 创建OSS客户端
        OSSClient ossClient = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        try {
            // 判断文件容器是否存在，不存在则创建
            if (!ossClient.doesBucketExist(BUCKET_NAME)) {
                ossClient.createBucket(BUCKET_NAME);
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(BUCKET_NAME);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(createBucketRequest);
            }
            // 创建文件路径
            String fileUrl = new String(new Date() + "/" + UUID.randomUUID());
            // 上传文件
            PutObjectResult result = ossClient.putObject(new PutObjectRequest(BUCKET_NAME, fileUrl, inputStream));
            if (null != result) {
                return FILE_HOST + fileUrl;
            }
        } catch (OSSException oe) {

        } catch (ClientException ce) {

        } finally {
            // 关闭OSS服务，一定要关闭
            ossClient.shutdown();
        }
        return null;

    }
}
